package ray.mgocc.parser;

import java.io.IOException;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

import ray.mgocc.Source;
import ray.mgocc.Score;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HtmlParser{
    private final static Log log = LogFactory.getLog(HtmlParser.class);

    static Pattern tab = Pattern.compile("<td class=\"tabContentsTitle\"><span>([^<]*)</span></td>");
    static Pattern value = Pattern.compile("<span class=\"value\">([^<]*)</span>");
    static Pattern title = Pattern.compile("<span class=\"title\">([^<]*)</span>");
    static String WEEK_TOTAL = "Weekly Total";
    static String CUMULATIVE_TOTAL = "Cumulative Total";
    static Pattern total = Pattern.compile("<span class=\"button\">("+WEEK_TOTAL+"|"+CUMULATIVE_TOTAL+")</span>");
    public Score parse(Source source){
        return parse(new InputStreamReader(source.getStream()));
    }
    public Score parse(Reader source){
        Score score = new Score();
        BufferedReader in = new BufferedReader(source);
        String line = null;
        try{
            boolean rule = true;
            while(null!=(line=in.readLine())){
                Matcher matcher;
                matcher = tab.matcher(line);
                if(matcher.find()){
                    String title = matcher.group(1);
                    log.debug("tab="+title);
                    score.setName(title);
                    if("Title History".equals(title) ||
                       "Awards History".equals(title) ||
                       "Official Game History".equals(title)){
                        break;
                    }
                    if("Personal Scores".equals(title)){
                        score.rulefull = true;
                    }
                }
                matcher = title.matcher(line);
                if(matcher.find()){
                    String title = matcher.group(1);
                    log.debug("title="+title);
                    score.pushTitle(title);
                }
                matcher = value.matcher(line);
                if(matcher.find()){
                    String value = matcher.group(1);
                    log.debug("value="+value);
                    try{
                        score.push(value);
                    }catch(RuntimeException e){
                        log.error("parse fail:"+line,e);
                        throw e;
                    }
                }
                matcher = total.matcher(line);
                if(matcher.find()){
                    String which = matcher.group(1);
                    boolean isWeek = false;
                    // 逆へのリンクなので注意。
                    if(WEEK_TOTAL.equals(which)){
                        isWeek = false;
                    }else if(CUMULATIVE_TOTAL.equals(which)){
                        isWeek = true;
                    }else{
                        throw new RuntimeException("bug");
                    }
                    score.setWeekly(isWeek);
                }
            }
        }catch(IOException e){
            log.error("parse error.",e);
        }
        return score;
    }
}
